﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class AddContactPoint : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            if (!IsPostBack)
            {
                IQueryable<codeContactPointType> c = from cpt in _sme.codeContactPointType orderby cpt.Description select cpt;

                cmbContactPointType.DataSource = c.ToArray();
                cmbContactPointType.DataTextField = "Description";
                cmbContactPointType.DataValueField = "ContactPointTypeCode";
                cmbContactPointType.DataBind();
            }
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            tblContactPoint newCP = new tblContactPoint();
            newCP.ContactPointTypeCode = cmbContactPointType.SelectedValue;
            newCP.PersonID = int.Parse(Request.QueryString["PersonID"]);
            //newCP.ContactPointTypeReference = new System.Data.Objects.DataClasses.EntityReference<codeContactPointType>() 
            //    { EntityKey = new System.Data.EntityKey("SalesManagerEntities.codeContactPointType", "ContactPointTypeCode", cmbContactPointType.SelectedValue) };
            //newCP.PersonReference = new System.Data.Objects.DataClasses.EntityReference<tblPerson>() 
            //    { EntityKey = new System.Data.EntityKey("SalesManagerEntities.tblPerson", "PersonID", int.Parse(Request.QueryString["PersonID"])) };
            newCP.Value = txtContactPointValue.Text;
            _sme.tblContactPoint.Add(newCP);
            //_sme.AddTotblContactPoint(newCP);
            //_sme.SaveChanges();

            if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack", 
                    "<script  language='javascript' type='text/javascript'>" +
                    "opener.__doPostBack('contactPointPanel', ''); window.close('fred');" +
                    "</script>"
                    );
        }
}
}
